<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="params.customerName" placeholder="用户姓名" style="width: 200px;" class="filter-item" clearable @keyup.enter.native="fetchData" />
      <el-input v-model="params.customerPhone" placeholder="手机号" style="width: 200px;margin-left: 10px;" class="filter-item" clearable @keyup.enter.native="fetchData" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-left: 10px;" @click="fetchData">
        搜索
      </el-button>
      <el-button type="success" icon="el-icon-warning-outline" class="filter-item" @click="resetForm()">重置</el-button>

    </div>
    <div class="filter-container">
      <el-button class="filter-item" type="primary" icon="el-icon-edit" @click="editOrAdd(0,0)">
        添加
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="头像"  align="center">
        <template slot-scope="scope">
          <img class="avart" :src="scope.row.avart" alt="">
        </template>
      </el-table-column>
      <el-table-column label="名称"  align="center">
        <template slot-scope="scope">
          {{ scope.row.customerName }}
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center">
        <template slot-scope="scope">
          {{ scope.row.customerPhone }}
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center">
        <template slot-scope="scope">
          {{ scope.row.customerType?scope.row.customerType:'普通用户' }}
        </template>
      </el-table-column>
      <el-table-column label="导入来源" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.source==0">订单自动导入</div>
          <div v-if="scope.row.source==1">手动导入</div>
        </template>
      </el-table-column>
      <el-table-column label="预付款金额" align="center">
         <template slot-scope="scope">
          <div v-if="scope.row.status==0" style="color:green;cursor:pointer;" @click="Admin(scope.row)">开启</div>
          <div v-if="scope.row.status==1" style="color:red;cursor:pointer;" @click="Admin(scope.row)">关闭</div>
        </template>
      </el-table-column>
            <el-table-column label="充值金额" align="center">
        <template slot-scope="scope">
            {{ scope.row.money }}
          <div v-if="scope.row.status==1" style="color:green;cursor:pointer;display: inline-block;margin-left:10px" @click="reCharge(scope.row)">充值</div>
          <div  style="color:blue;cursor:pointer;display:inline-block;margin-left:10px" @click="toPath(scope.row)">充值记录</div>
        </template>

      </el-table-column>
      <el-table-column align="center" prop="created_at" label="操作">
        <template slot-scope="scope">
          <el-row>
            <el-button type="text" @click="editOrAdd(1,scope.row)">编辑</el-button>
            <el-button  type="text" style="color:red" @click="deleteUser(scope.row.id)">删除</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="params.pageNum" :limit.sync="params.pageSize" @pagination="fetchData" />

    <el-dialog :title="dialogTitles[dialogIndex]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="adminData" label-width="100px" style="margin-left:100px;margin-right: 100px">
        <el-form-item label="头像" prop="avart">
          <el-upload
            class="avatar-uploader"
            :action="action"
            :show-file-list="false"
            :on-success="handleAvatarSuccess">
            <img v-if="adminData.avart" :src="adminData.avart" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
        </el-form-item>
        <el-form-item label="名称" prop="customerName">
          <el-input v-model="adminData.customerName"  placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="手机号" prop="customerPhone">
          <el-input v-model="adminData.customerPhone"  placeholder="请输入手机号"/>
        </el-form-item>
        <el-form-item label="类型" prop="customerType">
          <el-input v-model="adminData.customerType" placeholder="请输入类型"/>
        </el-form-item>
        <el-form-item label="导入来源" prop="source">
            <el-select v-model="adminData.source" class="filter-item" style="width:100%" placeholder="请选择导入来源">
              <el-option key="0" label="订单制动导入" :value="0" />
              <el-option key="1" label="手动增加" :value="1" />
          </el-select>
        </el-form-item>
          <el-form-item label="预付款金额" prop="status">
            <el-select v-model="adminData.status" class="filter-item" style="width:100%" placeholder="请选择预付款金额状态">
              <el-option key="0" label="关闭" :value="0" />
              <el-option key="1" label="开启" :value="1" />
            </el-select>
        </el-form-item>
          <el-form-item label="充值金额" prop="money">
          <el-input v-model="adminData.money" placeholder="请输入充值金额" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel()">
          取消
        </el-button>
        <el-button type="primary" @click="save">
          确定
        </el-button>
      </div>
    </el-dialog>



    <el-dialog title="充值金额" :visible.sync="czShow">
      <el-form>
    <el-form-item label="金额" label-width="140px">
      <el-input v-model="money" type="number" style="width:80%" placeholder="请输入充值金额"></el-input>
    </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
    <el-button @click="czShow = false">取 消</el-button>
    <el-button type="primary" @click="czOk">确 定</el-button>
  </div>
</el-dialog>
  </div>
</template>

<script>
import { addUser,getUser,deleteUser,Recharge } from '@/api/user'
import Pagination from '@/components/Pagination'
import waves from '@/directive/waves'
import Calendar from '@/components/Cascader'
export default {
  components: { Pagination, Calendar },
  directives: { waves },
  data() {
    return {
      id:'',
      action:process.env.VUE_APP_BASE_API+"/sysUser/upImgs.json",
      list: [],
      listLoading: true,
      total: 10,
      dialogTitles: ['添加', '编辑'],
      dialogIndex: 0,
      dialogFormVisible: false,
      czShow:false,
      money:'',
      adminData: {},
      params: {
        pageNum: 1,
        pageSize: 10,
        customerPhone: '',
        customerName: ''
      },
      rules: {
         avart: [{ required: true, message: '请上传头像', trigger: 'blur' }],
          customerName: [{ required: true, message: '请输入名称', trigger: 'blur' }],
          customerPhone: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
          customerType: [{ required: true, message: '请输入类型', trigger: 'blur' }],
            source: [{ required: true, message: '请选择数据来源', trigger: 'blur' }],
          status: [{ required: true, message: '请选择预付款金额状态', trigger: 'blur' }],
          money: [{ required: true, message: '请输入充值金额', trigger: 'blur' }],

      },
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 充值操作
    reCharge(obj){
      console.log(obj)
      this.money=''
      this.czShow=true
      this.id=obj.id
    },
    czOk(){
      if(this.money==''){
        this.$message.error('充值金额不能为空')
        return
      }
      let obj={
        customerId:this.id,
        money:this.money
      }
      Recharge(obj).then(res=>{
        this.$message.success('充值成功')
        this.czShow=false
        this.fetchData()
      })
    },
    // 跳转充值记录
    toPath(obj){
      this.$router.push('/Recharge?id='+obj.id)
    },
    // 图片上传
      handleAvatarSuccess(res, file) {
        this.adminData.avart='http://39.105.201.40:8081'+res.src;
      },
    // 关闭开启
    Admin(obj){
      this.$confirm('此操作将'+obj.status==0?'关闭':'开启'+'预付款金额, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            delete obj.createTime
             delete obj.updateTime
          if(obj.status==0){
              obj.status=1
          }else{
            obj.status=0
          }
            addUser(obj).then(res=>{
              this.$message({
              type: 'success',
              message: '修改成功!'
            });
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消修改'
          });          
        });

    },
    fetchData() {
      this.listLoading = true
      getUser(this.params).then(response => {
        this.total = response.total
        this.list = response.rows
        console.log(this.list)
        this.listLoading = false
      })
    },
    cancel() {
      this.dialogFormVisible = false
    },
    editOrAdd(index, item) {
      this.adminData = Object.assign({}, item)
      if (index === 1) {
        delete this.adminData.createTime
        delete this.adminData.updateTime
      }
      this.dialogIndex = index
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    save() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          addUser(this.adminData).then(res => {
            this.dialogFormVisible = false
            this.$message({
              type: 'success',
              message: this.dialogTitles[this.dialogIndex] + '成功!'
            })
            this.fetchData()
          })
        }
      })
    },
    deleteUser(id) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteUser({id:id}).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.fetchData()
        })
      })
    },
    resetForm() {
      this.params = {
        pageNum: 1,
        pageSize: 10
      }
      this.$nextTick(() => {
        this.fetchData()
      })
    }
  }
}
</script>
<style lang="scss" scoped>
    .avart{
      width: 50px;
      height: 50px;
      border-radius: 50%;
    }
    /deep/.el-dialog{
      width:40%
    }
    /deep/.el-upload{
      border: 1px solid #d9d9d9;
      border-radius: 5px;
      margin: 0 auto;
    }
     .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
</style>
